<?php
// +----------------------------------------------------------------------
// | zlserver [ 小程序 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\job\export;

use think\queue\Job;
use app\common\model\Ietask;
use think\facade\Log;


class BillPayments
{
    //执行导出任务
    public function exec(Job $job, $params)
    {
        $ietaskModle = new Ietask();
        $billPayments = new \app\common\model\BillPayments();
        $header = $billPayments->csvHeader();

        // 构造表头
        $billInfo = [];
        foreach ($header as $key => $val) {
            $billInfo['header'][$key] = $val['desc'];
        }
        $filter = json_decode($params['params'], true);

        if (isset($filter['ids'])) {
            if ($filter['ids']) {
                $filter['id'] = explode(',', $filter['ids']);
            }
            unset($filter['ids']);
        }

        // 查询导出内容
        $billData = $billPayments->getCsvData($filter);
        if ($billData['status']) {
            $body = $billData['data'];
            $billInfo['body'] = $body;

            $template = config('zlserver.billpayments_export_templete');  //使用模板
            $objPHPExcel = \PHPExcel_IOFactory::load($template);     //加载excel文件,设置模板 
            $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);  //设置保存版本格式  

            //接下来就是写数据到表格里面去  
            $objActSheet = $objPHPExcel->getActiveSheet();
            $i = 2;  
 
            foreach ($body as $key => $val) {
                //这里是设置单元格的内容  
                $objActSheet->setCellValue('A'.$i, $val['payment_id']); 
                $objActSheet->setCellValue('B'.$i, $val['status']); 
                $objActSheet->setCellValue('C'.$i, $val['payment_code']); 
                $objActSheet->setCellValue('D'.$i, $val['type']); 
                $objActSheet->setCellValue('E'.$i, $val['user_id']); 
                $objActSheet->setCellValue('F'.$i, $val['money']); 
                $objActSheet->setCellValue('G'.$i, $val['gift']); 
                $objActSheet->setCellValue('H'.$i, $val['trade_no']); 
                $objActSheet->setCellValue('I'.$i, $val['utime']); 
                $i++;
            }
            
            $file_name = "billpayments-xls-" . date("YmdHis", time()) . ".xls";
            $filePath  = defined("FILE_PATH") ? FILE_PATH : ROOT_PATH . 'public' . DS . 'static' . DS . 'file' . get_hash_dir($file_name);
            if (!is_dir($filePath)) {
                @mkdirs($filePath, 0777, true);
            } 
            $fullFilePath = $filePath . $file_name;
            // 1.保存至本地Excel表格  
            $objWriter->save($fullFilePath);
  
            $uData['file_name'] = $file_name;
            $uData['count'] = count($body);
            $uData['message'] = '导出成功';
            $uData['file_size'] = getRealSize(filesize($fullFilePath));
            $uData['file_path'] = $fullFilePath;
            $uData['status'] = $ietaskModle::EXPORT_SUCCESS_STATUS;
            $uData['utime'] = time();
            $ietaskModle->update($uData, ['id' => $params['task_id']]);
            
            $job->delete();
        } else {
            //失败，导出失败
            $uData['status'] = $ietaskModle::EXPORT_FAIL_STATUS;
            $uData['message'] = $billData['msg'];
            $uData['utime'] = time();
            $ietaskModle->update($uData, ['id' => $params['task_id']]);
        }
        if ($job->attempts() > 3) {
            $uData['status'] = $ietaskModle::EXPORT_FAIL_STATUS;
            $uData['message'] = '导出执行失败';
            $uData['utime'] = time();
            $ietaskModle->update($uData, ['id' => $params['task_id']]);
            $job->delete();
        }
        /*$job->release($delay); //$delay为延迟时间*/
    }

    public function failed($data)
    {

        // ...任务达到最大重试次数后，失败了
    }
}